package com.example.zqh.controller;


import com.example.zqh.model.RespBean;
import com.example.zqh.pojo.User;
import com.example.zqh.pojo.params.*;
import com.example.zqh.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zqh
 * @since 2022-09-10
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户表的增删改查")
public class UserController {

    @Autowired
    UserService userService;


    /**
      * @Author: zqh
      * @Date:
      * @Description: 用户登录接口
    */
    @ApiOperation(value = "用户登录接口，登录成功返回token（放行）")
    @PostMapping("login")
    public RespBean login(@RequestBody Login login){
        return userService.login(login);
    }
    
    /**
      * @Author: zqh
      * @Date: 
      * @Description: 获取当前登录用户的信息
    */
    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("getInfo")
    public RespBean getInfo(Principal principal){
        return userService.getInfo(principal);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 用户注册
    */
    @ApiOperation(value = "用户注册接口（用户名，性别，电话，密码）（放行）")
    @PostMapping("register")
    public RespBean registeredUser(@RequestBody RegisterParams registerParams){
        return userService.registeredUser(registerParams);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 用户查询接口
    */
    @ApiOperation(value = "用户查询接口（不放行，管理员查询)")
    @GetMapping("userQuery")
    public RespBean userQueryInterface(Integer page,Integer size){
        return userService.userQueryInterface(page,size);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 用户删除接口
    */
    @ApiOperation(value = "用户删除接口，需要管理员权限")
    @DeleteMapping("userDeleteInterface")
    @PreAuthorize("hasRole('ROLE_admin')")
    public RespBean userDeleteInterface(Integer id){
        return userService.userDeleteInterface(id);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 用户信息修改接口，管理员接口
    */
    @ApiOperation(value = "用户信息修改接口，管理员接口")
    @PreAuthorize("hasRole('ROLE_admin')")
    @PostMapping("adminModifyUserInformation")
    public RespBean adminModifyUserInformation(@RequestBody User user){
        return userService.adminModifyUserInformation(user);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 用户修改密码接口
    */
    @ApiOperation(value = "用户修改密码接口,登录用户自己使用（old用来和数据库中的密码对比，new是将要修改的密码）")
    @PostMapping("userChangePasswordInterface")
    public RespBean userChangePasswordInterface(String password,Principal principal){
        return userService.userChangePasswordInterface(password,principal);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 用户修改用户名
    */
    @ApiOperation(value = "用户自己修改自己的用户名")
    @PostMapping("userChangeNameInterface")
    public RespBean userChangeNameInterface(String name,Principal principal){
        return userService.userChangeNameInterface(name,principal);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改自己的性别
    */
    @ApiOperation(value = "修改自己的性别")
    @PostMapping("userChangeSexInterface")
    public RespBean userChangeSexInterface(Integer sex,Principal principal){
        return userService.userChangeSexInterface(sex, principal);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 用户修改自己的头像
    */
    @ApiOperation(value = "修改自己的头像")
    @PostMapping("userChangeHeadurlInterface")
    public RespBean userChangeHeadurlInterface(String headUrl,Principal principal){
        return userService.userChangeHeadurlInterface(headUrl,principal);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改自己的电话号码
    */
    @ApiOperation(value = "修改自己的电话号码")
    @PostMapping("userChangePhoneInterface")
    public RespBean userChangePhoneInterface(String phone,Principal principal){
        return userService.userChangePhoneInterface(phone, principal);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改自己的年龄
    */
    @ApiOperation(value = "修改自己的年龄")
    @PostMapping("userChangeAgeInterface")
    public RespBean userChangeAgeInterface(Integer age,Principal principal){
        return userService.userChangeAgeInterface(age,principal);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 充值接口
    */
    @ApiOperation(value = "充值接口")
    @PostMapping("rechargeInterface")
    public RespBean rechargeInterface(Integer money,Principal principal){
        return userService.rechargeInterface(money,principal);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改自我简介
    */
    @ApiOperation(value = "修改自我简介")
    @PostMapping("userChangeIntroInterface")
    public RespBean userChangeIntroInterface(String intro,Principal principal){
        return userService.userChangeIntroInterface(intro,principal);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改自我地址
    */
    @ApiOperation(value = "修改自我地址")
    @PostMapping("modifySelfAddress")
    public RespBean modifySelfAddress(String address,Principal principal){
        return userService.modifySelfAddress(address,principal);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 获取所有用户的数量
    */
    @ApiOperation(value = "获取所有用户的数量，放行")
    @GetMapping("getAllUserNumber")
    public RespBean getTheCountOfAllUsers(){
        return userService.getTheCountOfAllUsers();
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 模糊查询用户
    */
    @ApiOperation(value = "模糊查询用户，放行")
    @GetMapping("fuzzyQuery")
    public RespBean fuzzyQueryUsersByZ(String oneString){
        return userService.fuzzyQueryUsersByZ(oneString);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过性别查询不同性别用户的数量
    */
    @ApiOperation(value = "通过性别查询不同用户的数量（放行）")
    @GetMapping("getNumberBySex")
    public RespBean getNumberBySex(Integer sex){
        return userService.getNumberBySex(sex);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过性别查询id，并且带分页查询
    */
    @ApiOperation(value = "通过性别查询用户，并且带分页查询(放行）")
    @GetMapping("getUsersBySexAndPageing")
    public RespBean getUsersBySexAndPageing(Integer sex,Integer page,Integer size){
        return userService.getUsersBySexAndPageing(sex,page,size);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过id查询用户的信息
    */
    @ApiOperation(value = "通过id查询用户的信息,需要管理员权限")
    @GetMapping("queryUserInformationById")
    @PreAuthorize("hasRole('ROLE_admin')")
    public RespBean queryUserInformationById(Integer id){
        return userService.queryUserInformationById(id);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 后台管理系统登录接口
    */
    @ApiOperation(value = "后台管理系统登录系统(放行）")
    @PostMapping("AdminLogin")
    public RespBean AdminLogin(@RequestBody Login login){
        return userService.adminLogin(login);
    }





}

